package com.th.supcom.pds.drugsupply.client.widget;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.event.dom.client.KeyCodes;
import com.th.supcom.framework.dict.web.gwt.client.DictComboBox;
import com.th.supcom.pds.drugsupply.client.common.PdsPropertyMaitainModuleEvents;
import com.th.supcom.pds.drugsupply.client.model.DrugSearchModel;

/**
 * 供要属性维护查询面板
 * 
 * @author Zli
 */
public class DrugMaintainSearchPanel extends FieldSet
{

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";
    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";
    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
    /** 查询按钮. */
    private Button m_btn;
    /** 药品分类. */
    private DictComboBox m_phamCateCode;
    /** 药品名称. */
    private TextField <String> m_phamName;

    /** 摆药属性. */
    private DictComboBox m_putDrugMode;

    /** 是否供药. */
    private DictComboBox m_supplyIndicator;

    /** 发药方式. */
    private DictComboBox m_svrModeT;

    /** 贵重药品. */
    private DictComboBox m_value;

    public DrugMaintainSearchPanel ()
    {
        setHeading ("查询面板");
        initUI ();
        addEventListeners ();
    }

    /** 添加组件事件. */
    private void addEventListeners ()
    {
        m_btn.addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                final AppEvent event = new AppEvent (PdsPropertyMaitainModuleEvents.QUERY_INFO);
                Dispatcher.forwardEvent (event);
            }
        });
        m_phamName.addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (final ComponentEvent event)
            {
                super.componentKeyDown (event);
                if (event.getKeyCode () == KeyCodes.KEY_ENTER)
                {
                    final AppEvent event2 = new AppEvent (PdsPropertyMaitainModuleEvents.QUERY_INFO);
                    Dispatcher.forwardEvent (event2);
                }
            }
        });
    }

    /**
     * 信息查询条件
     * 
     * @return
     */
    public DrugSearchModel getFormdata ()
    {
        final DrugSearchModel drugInfoMode = new DrugSearchModel ();
        if (m_phamCateCode.getValue () != null)
        {
            drugInfoMode.setPhamCateCode (m_phamCateCode.getValue ().getDictItemCode ());
        }
        if (m_svrModeT.getValue () != null)
        {
            drugInfoMode.setSvrModeTypeId (m_svrModeT.getValue ().getDictItemCode ());

        }
        if (m_putDrugMode.getValue () != null)
        {
            drugInfoMode.setPutDrugMode (m_putDrugMode.getValue ().getDictItemCode ());
        }
        if (m_value.getValue () != null)
        {
            drugInfoMode.setExpensive (m_value.getValue ().getDictItemCode ());
        }
        if (m_supplyIndicator.getValue () != null)
        {
            drugInfoMode.setSupplyIndicator (m_supplyIndicator.getValue ().getDictItemCode ());
        }
        if (m_phamName.getValue () != null)
        {
            drugInfoMode.setPhamName (m_phamName.getValue ());
        }
        return drugInfoMode;
    }

    private DictComboBox initDictCoboBox (final String code, final String labelName)
    {
        final DictComboBox comboBox = new DictComboBox (code);
        comboBox.setFieldLabel (labelName);
        comboBox.setEmptyText ("全部");
        comboBox.setTriggerAction (TriggerAction.ALL);
        comboBox.setUseQueryCache (false);
        return comboBox;
    }

    private void initUI ()
    {
        final FormData formData = new FormData ("100%");
        final LayoutContainer main = new LayoutContainer ();
        main.setLayout (new ColumnLayout ());

        /** one. */
        final LayoutContainer one = new LayoutContainer ();
        final FormLayout layout1 = new FormLayout ();
        layout1.setLabelWidth (55);
        one.setLayout (layout1);

        m_phamCateCode = initDictCoboBox ("TJ0023", "用药分类");
        m_putDrugMode = initDictCoboBox ("PDS0001", "摆药属性");
        one.add (m_phamCateCode, formData);
        one.add (m_putDrugMode, formData);

        /** two. */
        final LayoutContainer two = new LayoutContainer ();
        final FormLayout layout2 = new FormLayout ();
        layout2.setLabelWidth (55);
        two.setLayout (layout2);
        two.setStyleAttribute ("padding-left", "20px");

        m_phamName = new TextField <String> ();
        m_phamName.setFieldLabel ("药品名称");
        m_value = initDictCoboBox ("DMS0011", "贵重药品");
        two.add (m_phamName, formData);
        two.add (m_value, formData);

        /** three. */
        final LayoutContainer three = new LayoutContainer ();
        final FormLayout layout3 = new FormLayout ();
        layout3.setLabelWidth (55);
        three.setLayout (layout3);
        three.setStyleAttribute ("padding-left", "20px");

        m_svrModeT = initDictCoboBox ("DMS0009", "发药方式");
        m_supplyIndicator = initDictCoboBox ("DMS0011", "是否供药");
        three.add (m_svrModeT, formData);
        three.add (m_supplyIndicator, formData);

        /** four. */
        final LayoutContainer four = new LayoutContainer ();
        final FormLayout layout4 = new FormLayout ();
        layout4.setLabelWidth (55);
        four.setLayout (layout4);
        four.setStyleAttribute ("padding-left", "20px");

        m_btn = new Button ("查询");
        m_btn.setWidth (80);
        four.add (m_btn);

        main.add (one, new ColumnData (.25));
        main.add (two, new ColumnData (.25));
        main.add (three, new ColumnData (.25));
        main.add (four, new ColumnData (.25));
        this.setLayout (new FitLayout ());
        this.add (main);
    }
}
